package com.clstu.datasource;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.testng.annotations.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.util.Properties;

/**
 * 测试Druid连接池的使用(集众多连接池的优点于一身)
 */
public class Druid_ {

    @Test//用druid获得连接
    public void method() throws Exception {
        //得到配置文件的properties对象
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);//传入properties对象
        /*
            //通过DruidDataSourceFactory对象得到dataSource
         */
        long start = System.currentTimeMillis();//测试性能
        for (int i = 0; i < 500000; i++) {
            Connection connection = dataSource.getConnection();//得到连接
//            System.out.println("连接成功");
            connection.close();//这里的close并不是真正的关闭掉了连接,而是放回到了连接池
        }
        long end = System.currentTimeMillis();
//        System.out.println("druid连接池5000次耗时:==="+(end-start)+"ms");//346ms(与c3p0差不多
        System.out.println("druid连接池500000次耗时:==="+(end-start)+"ms");//467ms(远高于c3p0

    }
}
